﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellDynLib_h
#define CellDynLib_h

#include "CellPrerequisites.h"

#if CELL_PLATFORM == CELL_PLATFORM_WIN32
#    define DYNLIB_HANDLE hInstance
#    define DYNLIB_LOAD( a ) LoadLibraryEx( a, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
#    define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
#    define DYNLIB_UNLOAD( a ) !FreeLibrary( a )

struct HINSTANCE__;
typedef struct HINSTANCE__* hInstance;

#elif CELL_PLATFORM == CELL_PLATFORM_LINUX
#    define DYNLIB_HANDLE void*
#    define DYNLIB_LOAD( a ) dlopen( a, RTLD_LAZY | RTLD_GLOBAL)
#    define DYNLIB_GETSYM( a, b ) dlsym( a, b )
#    define DYNLIB_UNLOAD( a ) dlclose( a )

#elif CELL_PLATFORM == CELL_PLATFORM_APPLE || CELL_PLATFORM == CELL_PLATFORM_IOS
#    define DYNLIB_HANDLE void*
#    define DYNLIB_LOAD( a ) mac_loadDylib( a )
#    define DYNLIB_GETSYM( a, b ) dlsym( a, b )
#    define DYNLIB_UNLOAD( a ) dlclose( a )

#elif CELL_PLATFORM == CELL_PLATFORM_SYMBIAN
#    define DYNLIB_HANDLE void*
#    define DYNLIB_LOAD( a ) 0
#    define DYNLIB_GETSYM( a, b ) 0
#    define DYNLIB_UNLOAD( a ) 0

#endif

namespace cell
{
/** \addtogroup Core
	*  @{
	*/
/** \addtogroup General
	*  @{
	*/

/** 动态库运行时对象。
@author
	Jiangwei Xu
*/
class _CellExport DynLib : public DynLibAlloc
{
public:
	/** 默认构造函数，只能由 DynLibManager 调用。
	@warning
		客户程序不能直接调用。
	*/
	DynLib(const String& name);

	/** 默认析构函数。
	*/
	~DynLib(void);

public:
	/** 加载动态库。
	*/
	void load(void);
	/** 卸载动态库。
	*/
	void unload(void);
	/** 返回动态库名称。
	*/
	inline const String& getName(void) const { return mName; }

	/** 从已加载的库里返回指定符号的地址。
	@param
		symbol 指定待查找的符号名。
	@return
		如果查找到符号，则返回符号句柄。
	@par
		如果查找符号失败，则返回 NULL 值。
	*/
	void* getSymbol(const String& symbol) const throw();

protected:
	String mName;

	/// 动态库句柄。
	DYNLIB_HANDLE mhInst;

protected:		
	/// 返回最近的错误信息。
	String dynlibError(void);
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellDynLib_h
